﻿Imports System.IO
Imports Training.Common

Partial Public Class AddFileForm
    Inherits System.Web.UI.Page
    Public vid As Integer ' videoid

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    ''' <summary>
    ''' upload the file and insert record
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnAddFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddFile.Click
        UploadFile()
    End Sub

    ''' <summary>
    '''  upload the file
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UploadFile()
        Dim fn As String ' file name
        Dim ct As String ' file content
        Dim fz As String ' file size
        Dim fileUploaded As Boolean

        ' initial value of fileUploaded
        fileUploaded = False

        fn = FileUpload1.PostedFile.FileName
        ct = FileUpload1.PostedFile.ContentType
        fz = FileUpload1.PostedFile.ContentLength

        ' define path to upload folder
        Dim p As String
        p = Server.MapPath("../video")

        Try
            ' upload the actual file
            FileUpload1.PostedFile.SaveAs(p & "\" & System.IO.Path.GetFileName(fn.ToString()))
            ' set fileUploaded
            fileUploaded = True

            ' proceed with data insert if upload was successful
            If fileUploaded Then
                ' get file type and path
                Dim typ As String ' type of file
                Dim pth As String ' path to file
                typ = Right(fn, 4)
                pth = System.IO.Path.GetFileName(fn.ToString())

                ' get video id
                vid = AddNewVideo(tbCname.Text)

                ' determine type
                Select Case typ
                    Case ".pdf"
                        typ = ".pdf"
                    Case ".swf"
                        typ = ".swf"
                    Case ".wmv"
                        typ = ".wmv"
                    Case Else
                        lblReturn.Text = "File type is not supported. Upload aborted."
                        Exit Sub
                End Select

                ' insert video file information
                If AddVideoFile(vid, tbDesc.Text, typ, pth) = 0 Then
                    lblReturn.Text = "File Uploaded<br />File Name: " & fn & "<br />File Type: " & ct & "<br />File Size: " & fz
                    ' everything was successful, continue
                    hfVID.Value = vid
                    add.Visible = False ' hide upload form
                    btnAddFile.Visible = False ' hide upload button
                    after.Visible = True ' show association form
                    btnAssoc.Visible = True ' show association button
                Else
                    lblReturn.Text = "Error inserting data."
                End If
            Else
                ' upload failed, notify user
                lblReturn.Text = "File was not uploaded. Insert aborted."
            End If
        Catch ex As Exception
            ' also log error here
            lblReturn.Text = ex.ToString
        End Try

    End Sub

    ''' <summary>
    ''' associate file with module
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnAssoc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAssoc.Click
        AddVideoToModule(ddlModule.SelectedValue, hfVID.Value)
        after.Visible = False ' show association form
        btnAssoc.Visible = False ' show association button
        lblReturn.Text = "File has been Associated to Module"
    End Sub
End Class